import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import {
  ProcurementCooperationUpdateSignStatus,
  ProcurementCooperationGetInfo,
  ProcurementCooperationGetAllByPagedAndSort
} from "@/api/stu/SupplyChainFinance"
import { FXQ } from "@/utils/public/FXQ"
import { useRoute } from "vue-router";
import { ref, reactive, nextTick, watch, onMounted, computed } from "vue";
import qs from "qs";
export function useEBusinessPurchase() {
  const route = useRoute();
  const isShowDetail = ref(false)
  const Visible = ref(false)
  const Vdialog = ref(false)
  const loading = ref<boolean>(false);
  const dataList = ref<any>([]);
  const procurementCooperationId = ref()
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns = [
    {
      label: "序号",
      type: "index",
      fixed: "left",
      minWidth: 50
    },
    {
      label: "供应商企业名称",
      prop: "secondParty",
    },
    {
      label: "签订协议日期",
      prop: "purchaseDate",
    },
    {
      label: "操作",
      fixed: "right",
      width: 140,
      slot: "operation"
    }
  ];

  const contract = reactive({
    seiId: "",
    ProcuringAgent: "xxxx电商企业公司",
    supplier: "",
    DateSigning: "",
    ContractNumber: "",
    FirstParty: "",
    SecondParty: "",
    SigningTime: "",
    createTime: ""
  })

  const isSeal = ref(false)


  // 签订
  async function ConcludeSign(row) {
    Vdialog.value = true
    procurementCooperationId.value = row.procurementCooperationId
    const data = qs.stringify({
      id: row.procurementCooperationId,
    })
    const res = await ProcurementCooperationGetInfo(data);
    if (res.success) {
      Vdialog.value = true
      contract.ProcuringAgent = "xxxx电商企业公司"
      contract.supplier = res.data.supplyName
      contract.DateSigning = res.data.purchaseDate
      contract.ContractNumber = res.data.contractNumber
    } else {
      ElMessage.error(res.msg)
    }
    nextTick(() => {
      const MYFXQ = FXQ.companySeal(contract.supplier, 0, 0, '', '');
      let view_FQX = document.querySelector("#view_FQX");
      let MYFQX = `<img src='${MYFXQ}' style="width:100%;height:100%"  />`
      view_FQX.innerHTML = MYFQX;
      if (row.signStatus == 2) {
        isShowDetail.value = true
        isSeal.value = true
        const MYFXQ = FXQ.companySeal(contract.ProcuringAgent, 0, 0, '', '');
        let view_FQX = document.querySelector("#view_FQXS");
        let MYFQX = `<img src='${MYFXQ}' style="width:100%;height:100%"  />`
        view_FQX.innerHTML = MYFQX;
      }
    })
  }

  function Signet() {
    // // 生成章
    const MYFXQ = FXQ.companySeal(contract.ProcuringAgent, 0, 0, '', '');
    let view_FQX = document.querySelector("#view_FQXS");
    let MYFQX = `<img src='${MYFXQ}' style="width:100%;height:100%"  />`
    view_FQX.innerHTML = MYFQX;
    isSeal.value = true
  }


  async function addCase() {
    if (!isSeal.value) {
      ElMessage.warning("甲方未盖章")
      return
    }
    const data = qs.stringify({
      id: procurementCooperationId.value
    })
    const res = await ProcurementCooperationUpdateSignStatus(data)
    if (res.success) {
      ElMessage.success('签订采购合作协议成功！');
      getClientBaseData()
      ClosePanelVisible()
    } else {
      ElMessage.error(res.msg)
    }
  }

  function ClosePanelVisible() {
    contract.supplier = ""
    contract.DateSigning = ""
    contract.ContractNumber = ""
    contract.FirstParty = ""
    contract.SecondParty = ""
    contract.SigningTime = ""
    Vdialog.value = false
    isShowDetail.value = false
    isSeal.value = false
  }


  // 分页获取采购合作协议列表
  async function getClientBaseData() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      SEIId: '',
    }
    const res = await ProcurementCooperationGetAllByPagedAndSort(data);
    if (res.success) {
      res.data.items.forEach(item => {
        item.purchaseDate = item.purchaseDate.substring(0, 10)
      })
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount
    } else {
      ElMessage.error(res.msg)
    }
  }
  function handleSizeChange() {
    getClientBaseData()
  }
  function handleCurrentChange() {
    getClientBaseData()
  }
  onMounted(() => {
    getClientBaseData()
  })

  return {
    Visible,
    columns,
    loading,
    dataList,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    ConcludeSign,
    Vdialog,
    contract,
    Signet,
    isSeal,
    ClosePanelVisible,
    addCase,
    isShowDetail
  }
}
